
package pions.view.calendars;

import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import pions.controller.Calendars;
import pions.controller.DropShifts;
import pions.controller.xml.XMLFactory;
import pions.controller.xml.XMLIterator;
import pions.view.AbstractXMLList;

/**
 *
 * 
 */
public class DropShift extends AbstractXMLList {

    /** Creates new form CreateDropShift */
    public DropShift() {
        initComponents();

        init(Calendars.getWorkSchedule());
    }

    private void init(XMLIterator<?> iter){
        while(iter.hasNext()){
            Document xml = iter.next();

            root = XMLFactory.getHead(xml, XMLFactory.CALENDAR);

            combobox_shifts.addItem(AbstractCalendarList.getComboBoxBuffer(root));
        }
    }

    private void display(Document xml){
        root = XMLFactory.getHead(xml, XMLFactory.CALENDAR);

        StringBuffer buffer = new StringBuffer();

        buffer.append(XMLFactory.TITLE + ": ");
        buffer.append(XMLFactory.getAttribute(root, XMLFactory.TITLE));
        buffer.append('\n');
        buffer.append(XMLFactory.TEXT + ": ");
        buffer.append(XMLFactory.getAttribute(root, XMLFactory.TEXT));
        buffer.append('\n');
        buffer.append(XMLFactory.START_TIME + ": ");
        buffer.append(XMLFactory.getAttribute(root, XMLFactory.START_TIME));
        buffer.append('\n');
        buffer.append(XMLFactory.END_TIME + ": ");
        buffer.append(XMLFactory.getAttribute(root, XMLFactory.END_TIME));

        textarea_details.setText(buffer.toString());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_title = new javax.swing.JLabel();
        label_directions = new javax.swing.JLabel();
        button_drop = new javax.swing.JButton();
        combobox_shifts = new javax.swing.JComboBox();
        scrollpane_details = new javax.swing.JScrollPane();
        textarea_details = new javax.swing.JTextArea();
        label_details = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(300, 400));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pions.PIONS.class).getContext().getResourceMap(DropShift.class);
        label_title.setFont(resourceMap.getFont("label_title.font")); // NOI18N
        label_title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_title.setText(resourceMap.getString("label_title.text")); // NOI18N
        label_title.setName("label_title"); // NOI18N

        label_directions.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label_directions.setText(resourceMap.getString("label_directions.text")); // NOI18N
        label_directions.setName("label_directions"); // NOI18N

        button_drop.setText(resourceMap.getString("button_drop.text")); // NOI18N
        button_drop.setName("button_drop"); // NOI18N
        button_drop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_dropActionPerformed(evt);
            }
        });

        combobox_shifts.setMaximumRowCount(10);
        combobox_shifts.setName("combobox_shifts"); // NOI18N
        combobox_shifts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combobox_shiftsActionPerformed(evt);
            }
        });

        scrollpane_details.setName("scrollpane_details"); // NOI18N

        textarea_details.setColumns(20);
        textarea_details.setEditable(false);
        textarea_details.setLineWrap(true);
        textarea_details.setRows(5);
        textarea_details.setWrapStyleWord(true);
        textarea_details.setName("textarea_details"); // NOI18N
        scrollpane_details.setViewportView(textarea_details);

        label_details.setText(resourceMap.getString("label_details.text")); // NOI18N
        label_details.setName("label_details"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(label_title, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addComponent(label_directions, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(combobox_shifts, 0, 280, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_details, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(button_drop, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollpane_details, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(label_title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_directions)
                .addGap(18, 18, 18)
                .addComponent(combobox_shifts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(label_details)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrollpane_details, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(button_drop, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void button_dropActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_dropActionPerformed
        if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(this,
                "Are you sure you want to send this drop shift request?",
                "Confirm Drop Shift",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE)) {
            DropShifts.sendDropShift();
        }
    }//GEN-LAST:event_button_dropActionPerformed

    private void combobox_shiftsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combobox_shiftsActionPerformed
        int index = combobox_shifts.getSelectedIndex();
        DropShifts.setCurrentShift(index);
        display(Calendars.getWorkShift(index));
    }//GEN-LAST:event_combobox_shiftsActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button_drop;
    private javax.swing.JComboBox combobox_shifts;
    private javax.swing.JLabel label_details;
    private javax.swing.JLabel label_directions;
    private javax.swing.JLabel label_title;
    private javax.swing.JScrollPane scrollpane_details;
    private javax.swing.JTextArea textarea_details;
    // End of variables declaration//GEN-END:variables

}
